package com.tcc.qa.test.util;

import org.apache.xpath.operations.String;
import org.openqa.jetty.html.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class ButtonFinder {
    public static WebElement getButtonById(WebDriver driver, String id) {
        return driver.findElement(By.id(id));
    }

    public static WebElement getSubmitButton(WebDriver driver, String name) {
        return driver.findElement(By.name(name));
    }

    public static WebElement getButtonByTitle(WebDriver driver, String title) {
        // return
        // driver.findElement(By.xpath("//button[(@type='BUTTON' or @type='button') and @class='ccBtn' and @title='"
        // + title + "']"));
        return driver
                .findElement(By
                        .xpath("//button[((@type='BUTTON' or @type='button') and @class='ccBtn' and @title='"
                                + title
                                + "')or(@type='button' and @title='"
                                + title + "')]"));
    }

    public static WebElement getButtonByValue(WebDriver driver, String name) {
        List<WebElement> eles = driver.findElements(By
                .xpath("//button[@class='ccBtn']"));
        for (WebElement e : eles) {

            if (e.getText().equals(name)) {
                return e;
            }
        }
        return null;
    }

    public static WebElement getInputButtonByValue(WebDriver driver,
            String value) {
        return driver
                .findElement(By
                        .xpath("//input[((@type='BUTTON' or @type='button') and @class='ccBtn' and @value='"
                                + value
                                + "') or (@type='button' and @value='"
                                + value + "')]"));
    }

    public static WebElement getInputByValue(WebDriver driver, String value) {
        return driver
                .findElement(By
                        .xpath("//input[(@type='BUTTON' or @type='button') and @class='ccBtn' and @value='"
                                + value + "']"));
    }

    public static WebElement getSubmitButtonByValue(WebDriver driver,
            String value) {
        return driver
                .findElement(By
                        .xpath("//input[(@type='SUBMIT' or @type='submit') and @class='ccBtn' and @value='"
                                + value + "']"));
    }

    public static WebElement getTableButton(WebDriver driver, String name) {
        List<WebElement> eles = driver
                .findElements(By
                        .xpath("//a[(@role='BUTTON' or @role='button') and @class='ccIcon']"));
        for (WebElement e : eles) {

            if (e.getText().equals(name)) {
                return e;
            }
        }
        return null;
        // null;
    }

    public static WebElement getLinkButton(WebDriver driver, String name) {
        List<WebElement> eles = driver.findElements(By
                .xpath("//a[@class='ccIcon']"));
        for (WebElement e : eles) {

            if (e.getText().equals(name)) {
                return e;
            }
        }
        return null;
    }

    public static WebElement getLinkButton(WebDriver driver, String name,
            String attribute, String contains) {
        List<WebElement> eles = driver.findElements(By
                .xpath("//a[@class='ccIcon']"));
        for (WebElement e : eles) {
            String v = e.getAttribute(attribute);
            if (v == null)
                v = "";
            if (e.getText().equals(name) && v.contains(contains)) {
                return e;
            }
        }
        return null;
    }

    public static List<WebElement> getLinks(WebDriver driver,
            String containsName, String attribute, String contains) {
        List<WebElement> eles = driver.findElements(By.xpath("//a"));
        List list = new ArrayList();
        for (WebElement e : eles) {
            String v = e.getAttribute(attribute);
            if (v == null)
                v = "";
            if (e.getText().contains(containsName) && v.contains(contains)) {
                list.add(e);
            }
        }
        return list;
    }

    public static boolean isDisplayed(WebElement btn) {

        if (btn == null)
            return false;
        return btn.isDisplayed();
    }

}
